package com.sxfq.loansupermarket.backsystem.modular.basicData.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.constant.tips.Tip;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.common.node.ZTreeNode;
import com.sxfq.loansupermarket.backsystem.common.persistence.dao.UserMapper;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.User;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.Convert;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.basicData.dao.OutpatientManagementDao;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.OutpatientManagementInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchOutpatientManagementInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IOutpatientManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 预约客户信息
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/outpatientManagement")
public class OutpatientManagementController extends BaseController {

    @Autowired
    IOutpatientManagementService service;

    @Autowired
    private OutpatientManagementDao dao;


    @Resource
    UserMapper userMapper;

    private String PREFIX = "/basicData/outpatientManagement/";

    /**
     * 跳转到预约客户管理页面
     */
    @RequestMapping("")
    public String outpatientManagement(Model model) {
        return PREFIX + "/outpatientManagement.html";
    }

    /**
     * 跳转到预约客户管理添加页面
     */
    @RequestMapping(value = "/outpatientManagement_add")
    public String outpatientManagementAdd() {
        return PREFIX + "outpatientManagement_add.html";
    }

    /**
     * 跳转到预约客户管理详情页面
     */
    @RequestMapping(value = "/outpatientManagement_update/{outpatientManagementId}")
    public String outpatientManagementUpdate(@PathVariable String outpatientManagementId, Model model) {
        if (ToolUtil.isEmpty(outpatientManagementId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        OutpatientManagementInfo outpatientManagementInfo = this.service.selectOneById(outpatientManagementId);
        model.addAttribute("outpatientManagementInfo", outpatientManagementInfo);
        return PREFIX + "outpatientManagement_edit.html";
    }

    /**
     * 预约客户管理列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object testrInfoList(SearchOutpatientManagementInfo info, HttpServletRequest request) {
        Page<OutpatientManagementInfo> page = new PageFactory<OutpatientManagementInfo>().defaultPage();
        try {
            List<OutpatientManagementInfo> list = service.selectPageListByQueryGetMap(page, info);
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     * 添加预约客户
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public int add(@Valid OutpatientManagementInfo info, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        return service.insert(info);
    }

    /**
     * 删除预约客户
     */
    @RequestMapping(value = "/delete/{id}")
    @ResponseBody
    public Tip delete(@PathVariable String id) throws Exception {
        service.delete(id);
        return SUCCESS_TIP;
    }

    /**
     * 修改考题
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public int update(@Valid OutpatientManagementInfo info, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        return service.update(info);
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public  List<Map<String,Object>> getList() {
        List<Map<String,Object>> list= service.list();
        return list;
    }

    @RequestMapping(value = "/getLists", method = RequestMethod.POST)
    @ResponseBody
    public  List<Map<String,Object>> getLists(String userName) {
        List<Map<String,Object>> list= service.lists(userName);
        return list;
    }

}
